const app = getApp();
import api from "../../utils/apiInstance"
import {  getUserInfo,setUserInfo } from '../../utils/store';
Page({
  data: {
    baseUrl: app.globalData.baseUrl,
    fileUrl: app.globalData.fileUrl,
    searchParam:{},
    sapId: '',
    cardList: [],
    courseIndex: null,
    projectType:0,
    isKeJianShow:false,
    isSignShow:false,
    dePartParams:{},
    userParams:{},
    dePartList:[],
    bigSearchShow:false,
    form:{
      name:getUserInfo()?.siocCnName,
      account:getUserInfo()?.siocPhone
    },
    currentDepart:"",
    currentDepartName:"",
    currentCardIndex:0,
    currentTrainId:'',
    currentCard:{}
  },
  async loadData () {
    var that = this;
    let res = [];
   
    res = await api.post(`/v1/openApi/trainer/needSignCourse`,{
      projectType:that.data.projectType||'',
      ...that.data.searchParam
    })
    
   
  var data = res.rows|| [res.data];

   if(data.length>0){
    for(let item of data){
      const params = {
        trainId:item.id,
        
        pass:1,
        
      }
      const userData = await this.loadUser(params)
      item.userData = userData.data.data
      item.currentDepart = this.data.currentDepart;
      item.currentDepartName = this.data.currentDepartName;
    }
   }  
  
    that.setData({
      cardList: data
    })

   
   
  },
  onLoad: async function (params) {
    const initParam = this.selectComponent("#searchTab").data
   
    this.setData({
      searchParam: {
        endTime:initParam.currentTime[1],
        startTime:initParam.currentTime[0],
        sign:initParam.signStatus
      },
      projectType:params.type,
      sapId: wx.getStorageSync('userinfo').siocSapNo
    })
    await this.loadDept();
    this.loadData();
    
  },

  startSign(){
    wx.navigateTo({
      url: `/pages/comWebView/comWebView?url=${encodeURIComponent(app.globalData.signUrl)}`
    });
  },

  nameChange(e){
    this.setData({
      ['form.name']:e.detail.value
    })
  },
  numAccount(e){
    this.setData({
      ['form.account']:e.detail.value
    })
  },
  //签名
  doSign(e){
    console.log('e111111',e)
    const that = this;
    if(that.data.form.name&&that.data.form.account){
      console.log(11111)
      that.sign(e);
    }else{
      console.log(2222)
      that.setData({
        isSignShow: true
      });
    }
  },

  async loadDept(){
    return new Promise(async (reslove,reject)=>{
      if(this.data.dePartList.length==0){
        let dePartRes = await api.get(`/v1/openApi/employee/search/departList`,{ ...this.data.dePartParams});
        this.setData({
          //默认部门
          currentDepart:dePartRes.data.find(item=>item.deptName == getUserInfo().siocDepartCode)?.id||dePartRes.data[0].id,
          dePartList:dePartRes.data,
          currentDepartName:dePartRes.data.find(item=>item.deptName == getUserInfo().siocDepartCode)?.deptName||dePartRes.data[0].deptName
        })
        reslove();
      }
    })
  },
  
  loadUser(params){
    return api.post(`/v1/openApi/staff/manager/list`,params);
  },

  doShowBigSearch(e){
   

    this.setData({  
      currentCardIndex:e.currentTarget?.dataset?.index,
      bigSearchShow:true,
      currentCard:e.currentTarget?.dataset?.card
    });
  },

  searchSelect(e){
    console.log('e',e);

    const cardList = JSON.parse(JSON.stringify(this.data.cardList))
    cardList[this.data.currentCardIndex].currentDepart=e.detail.id;
    cardList[this.data.currentCardIndex].currentDepartName=e.detail.label;

    const params = {
      trainId:this.data.currentCard.id,
      pass:1
    }
    this.loadUser(params);

  },

  async sign(e){
    if(!this.data.form.name){
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none',
        duration: 3000
      });
      return
    }
    if(!this.data.form.account){
      wx.showToast({
        title: '请输入手机号！',
        icon: 'none',
        duration: 3000
      });
      return
    }

    setUserInfo(Object.assign({},getUserInfo(),{siocPhone:this.data.form.account}))
    //调用签名接口
    const res = await api.post(`/v1/openApi/staff/examines/${this.data.examId}/?account=${this.data.form.account}&name=${this.data.form.name}&redirectUrl=${encodeURIComponent(this.data.baseDomain+'/#/webView?random='+new Date().getTime())}`, this.data.form)

    if(res.data){
    
      this.setData({
        
        isSignShow:false
      })

      wx.navigateTo({
        url: `/pages/comWebView/comWebView?url=${encodeURIComponent(res.data)}`
      });

    }
  },
  //搜索返回
  doSearch(res){
    let newParams = {
      endTime:res.detail.currentTime[1],
      startTime:res.detail.currentTime[0],
      sign:Boolean(+res.detail.signStatus),
      pass:Boolean(+res.detail.achievementType)
    }
    //全部要删去参数
    if(res.detail.achievementType=="2"){
      delete newParams.pass
    }
    if(res.detail.signStatus=="2"){
      delete newParams.sign
    }

    this.setData({
      searchParam: newParams
    })

    this.loadData();
  },

  
 
    
  
})